define(["drawable"], function(Drawable){
        
    
    Bouncer.prototype = new Drawable();
    
    function Bouncer(sprite) {             
        this.sprite = sprite;    
        this.maxX = undefined;
        this.speed = 5;
        this.init(sprite.width, sprite.height);
        
        var keyState = {
            right : false,
            left : false
        };
        
        var setKeyState = function(code, state){
            if(code == 39){
                keyState.right = state;
            }
            else if(code == 37){
                keyState.left = state;
            }
        };
        
        document.onkeydown = function(e){
            e.preventDefault();
            setKeyState(e.keyCode, true);
        };
        
        document.onkeyup = function(e){
            e.preventDefault();
            setKeyState(e.keyCode, false);
        };
        
        this.move = function(){
                            
            if(keyState.right){ 
                if(this.x <= (this.maxX - this.sprite.width-4)){
                    this.x += this.speed;
                }
            }
            else if(keyState.left){ // left
                if(this.x >= this.speed){
                    this.x -= this.speed;
                }
            }

        };
    }
    
    //Bouncer.prototype.collides = function() {};
    
    Bouncer.prototype.draw = function(context){
        
        context.clearRect(this.x-5, this.y, this.w+10, this.h);
        
        if(this.x === undefined){
            this.maxX = context.canvas.width;    
            this.x = (this.maxX-this.w)/2;
            this.y = context.canvas.height - context.canvas.height/5;
        }
        
        this.move();
        context.drawImage(this.sprite, this.x, this.y );

    };
    
    
    
    return Bouncer;
});